/**
 * 
 */
package com.vanstone.imgapp.img.services.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import com.vanstone.common.debug.MyAssert;
import com.vanstone.fs.FSManager;
import com.vanstone.fs.api.FileException;
import com.vanstone.fs.local.LocalFSFile;
import com.vanstone.fs.weedfs.WeedFSFile;
import com.vanstone.imgapp.api.img.AbstractArticle;
import com.vanstone.imgapp.api.img.Album;
import com.vanstone.imgapp.api.img.ArticleType;
import com.vanstone.imgapp.api.img.Category;
import com.vanstone.imgapp.api.img.Image;
import com.vanstone.imgapp.api.img.Tag;
import com.vanstone.imgapp.api.img.services.AlbumService;
import com.vanstone.imgapp.api.img.services.CategoryService;
import com.vanstone.imgapp.api.img.services.ImageFSBean;
import com.vanstone.imgapp.api.img.services.TagService;
import com.vanstone.imgapp.common.id.SystemIDSchema;
import com.vanstone.imgapp.common.id.services.IDService;
import com.vanstone.imgapp.img.dal.CmsCommonDao;
import com.vanstone.imgapp.img.dal.object.CmsArticleDO;
import com.vanstone.imgapp.img.dal.object.CmsArticleDetailDO;

/**
 * @author shipeng
 *
 */
@Service("albumService")
public class AlbumServiceImpl implements AlbumService {

  @Autowired
  private MongoTemplate mongoTemplate;
  @Autowired
  private IDService idService;
  @Autowired
  private CmsCommonDao cmsCommonDao;
  @Autowired
  private CategoryService categoryService;
  @Autowired
  private TagService tagService;

  @Override
  public Album addAlbum(long categoryID, String title, String brief, String content, List<String> tagnames, LocalFSFile coverLocalFSFile, List<ImageFSBean> imageFSBeans) throws FileException {
    Category category = this.categoryService.getCategory(categoryID);
    MyAssert.objectInitialized(category);
    Album album = new Album();
    album.setId(idService.nextID(SystemIDSchema.Cms_Article));
    album.setSysInsertTime(new Date());
    album.setTitle(title);
    album.setBrief(brief);
    album.setContent(content);
    album.setCategory(category);

    WeedFSFile coverFile = null;
    if (coverLocalFSFile != null) {
      coverFile = FSManager.getInstance().uploadToWeedFS(coverLocalFSFile.getFile());
    }
    album.setCoverFile(coverFile);
    if (imageFSBeans != null && imageFSBeans.size() > 0) {
      for (ImageFSBean temp_imagefsbean : imageFSBeans) {
        WeedFSFile weedFSFile = null;
        if (temp_imagefsbean.isLocalFSFile()) {
          weedFSFile = FSManager.getInstance().uploadToWeedFS(temp_imagefsbean.getLocalFSFile().getFile());
        } else {
          LocalFSFile temp_localfsfile = FSManager.getInstance().downloadImageToLocalFS(temp_imagefsbean.getWebURL());
          weedFSFile = FSManager.getInstance().uploadToWeedFS(temp_localfsfile.getFile());
        }
        Image image = new Image();
        image.setImageFile(weedFSFile);
        image.setContent(temp_imagefsbean.getContent());
        album.addImage(image);
      }
    }

    if (tagnames != null && tagnames.size() > 0) {
      for (String tagname : tagnames) {
        Tag tag = this.tagService.saveorget(tagname);
        album.addTag(tag);
      }
    }

    CmsArticleDO cmsArticleDO = BeanUtil.toCmsArticleDO(album);
    CmsArticleDetailDO cmsArticleDetailDO = new CmsArticleDetailDO();
    cmsArticleDetailDO.setId(album.getId());
    cmsArticleDetailDO.setContent(album.getContent());

    this.mongoTemplate.insert(cmsArticleDO);
    this.mongoTemplate.insert(cmsArticleDetailDO);
    this.cmsCommonDao.inc_category_imgcount(categoryID, 1);
    return album;
  }

  @Override
  public Album addAlbum(long categoryID, String title, String brief, String content, List<String> tagnames, List<ImageFSBean> imageFSBeans, boolean default_first_cover) throws FileException {
    LocalFSFile cover_localfsfile = null;
    if (imageFSBeans != null && imageFSBeans.size() > 0) {
      ImageFSBean imageFSBean = imageFSBeans.get(0);
      if (imageFSBean.isLocalFSFile()) {
        cover_localfsfile = imageFSBean.getLocalFSFile();
      } else {
        cover_localfsfile = FSManager.getInstance().downloadImageToLocalFS(imageFSBean.getWebURL());
      }
    }
    return this.addAlbum(categoryID, title, brief, content, tagnames, cover_localfsfile, imageFSBeans);
  }

  @Override
  public Album getAlbum(long id) {
    CmsArticleDO cmsArticleDO = this.mongoTemplate.findById(id, CmsArticleDO.class);
    if (cmsArticleDO == null) {
      return null;
    }
    if (cmsArticleDO.getArticleTypeCode() == null || !cmsArticleDO.getArticleTypeCode().equals(ArticleType.Album.getCode())) {
      throw new IllegalArgumentException();
    }
    CmsArticleDetailDO cmsArticleDetailDO = this.mongoTemplate.findById(id, CmsArticleDetailDO.class);
    Category category = this.categoryService.getCategory(cmsArticleDO.getCategoryId());
    List<Tag> tags = this.tagService.getTags(cmsArticleDO.getTagIDs());
    AbstractArticle article = BeanUtil.toArticle(cmsArticleDO, cmsArticleDetailDO, tags, category);
    return (Album) article;
  }

  @Override
  public Album updateAlbumBaseInfo(long id, String categoryID, String title, String brief, String content, List<String> tagnames) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Album updateAlbunmCoverInfo(long id, LocalFSFile localFSFile) throws FileException {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Album deleteAlbumCoverInfo(long id) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public void deleteAlbumImageFile(long id, String fileid) {
    // TODO Auto-generated method stub

  }

  @Override
  public Album appendAlbumImageFile(long id, LocalFSFile localFSFile) throws FileException {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public Album setImageFileToCoverFile(long id, String fileid) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public List<Album> getAlbumsByCategoryID(String categoryID, long offset, long limit) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public List<Album> getTotalAlbumsByCategoryID(String categoryID) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public List<Album> getAlbumsByTagID(long tagID, long offset, long limit) {
    // TODO Auto-generated method stub
    return null;
  }

  @Override
  public List<Album> getTotalAlbumsByTagID(long tagID, long offset, long limit) {
    // TODO Auto-generated method stub
    return null;
  }

}
